package com.netvine.iias;

import java.io.Serializable;

public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final int FAIL = 500;

    public static final int SUCCESS = 200;

    private int code = SUCCESS;

    private String msg = "操作成功";

    private T data;

    private Result() {
    }


    public static <T> Result<T> ok(T data) {
        Result<T> result=new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> ok(T data, String msg) {
        Result<T> result=new Result<>();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> ok() {
        return new Result<>();
    }

    public static <T> Result<T> okWithMsg(String msg) {
        Result<T> result=new Result<>();
        result.setMsg(msg);
        return result;
    }

    public boolean success() {
        return this.code == 200;
    }

    public static <T> Result<T> error() {
        Result<T> result=new Result<>();
        result.setCode(FAIL);
        result.setMsg("请求失败");
        return result;
    }

    public static <T> Result<T> error(T data) {
        Result<T> result=new Result<>();
        result.setCode(FAIL);
        result.setMsg("请求失败");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(int code, String msg) {
        Result<T> result=new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result=new Result<>();
        result.setCode(FAIL);
        result.setMsg(msg);
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
